<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    style="margin-top: -15px"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <lb-table
      :column="tableData.column"
      :data="tableData.data"
      pagination
      background
      border
      height="740"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[20, 40, 60, 80]"
      :pager-count="5"
      :current-page.sync="currentPage"
      :total="total"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @p-current-change="handleCurrentChange"
    >
    </lb-table>
    <operate :id="id" :visible.sync="show" @close="close"></operate>
  </lb-layout>
</template>

<script>
import { whiteBlackList, delWhiteBlack } from "@/api/sub/portrayal";
import operate from "@/views/configuration/arithmetic/portrayal/components/blackWhiteOperate";

export default {
  name: "network",
  dicts: [
    "energy_subarea_type", // 分区类别
    "energy_subarea_child_type", // 分区子类
    "white_black_flag",
  ],
  components: {
    operate,
  },
  data() {
    return {
      //弹窗
      id: "",
      show: false,
      apsForm: {
        formRef: "form",
        labelWidth: "100px",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            //这里是渲染查询表单的表头和类型的数据
            deviceName: {
              label: "用电设备:",
              typeName: "el-input",
              clearable: true,
              placeholder: "请输入",
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
            {
              label: "新增",
              type: "primary",
              typeName: "el-button",
              plain: true,
              size: "small",
              icon: "el-icon-plus",
              click: () => this.operate(""),
            },
          ],
        },
      },
      tableData: {
        column: [
          {
            prop: "subareaType",
            label: "分区类别",
            align: "center",
            width: "150",
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.energy_subarea_type}
                    value={scope.row.subareaType}
                  />
                </div>
              );
            },
          },
          {
            prop: "subareaChildType",
            label: "分区子类",
            align: "center",
            width: "150",
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.energy_subarea_child_type}
                    value={scope.row.subareaChildType}
                  />
                </div>
              );
            },
          },
          {
            prop: "deviceName",
            label: "用电设备",
            showOverflowTooltip: true,
          },
          {
            label: "常规能耗（kwh）",
            minWidth: 150,
            prop: "kwh",
            showOverflowTooltip: true,
          },
          {
            label: "常规日时长（h/d）",
            minWidth: 150,
            prop: "dayOfUseTime",
            showOverflowTooltip: true,
          },
          {
            label: "调幅比例（%）",
            minWidth: 150,
            prop: "amplitudeModulationPercent",
            render: (h, params) => {
              let num = Number(params.row.amplitudeModulationPercent);
              return h("div", {}, num);
            },
          },
          {
            prop: "whiteBlackFlag",
            label: "黑白名单标志",
            width: 150,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.white_black_flag}
                    value={scope.row.whiteBlackFlag}
                  />
                </div>
              );
            },
          },
          {
            label: "操作",
            width: "150",
            fixed: "right",
            render: (h, scope) => {
              return (
                <div>
                  {
                    <el-button
                      v-hasPermi={["device:networks:edit"]}
                      type="text"
                      size="small"
                      icon="el-icon-edit"
                      onClick={() => {
                        this.operate(scope.row.id);
                      }}
                    >
                      修改
                    </el-button>
                  }
                  {
                    <el-button
                      v-hasPermi={["device:networks:remove"]}
                      type="text"
                      size="small"
                      icon="el-icon-delete"
                      onClick={() => {
                        this.handleOperDelete(scope.row);
                      }}
                      style="color:red"
                    >
                      删除
                    </el-button>
                  }
                </div>
              );
            },
          },
        ],
        data: [],
      },
      currentPage: 1,
      pageSize: 20,
      total: 0,
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    operate(id) {
      this.id = id;
      this.show = true;
    },
    close(status) {
      this.show = false;
      setTimeout(() => {
        if (status) this.handlerSearchDetail();
      }, 200);
    },
    //删除
    handleOperDelete(row) {
      let that = this;
      const text = "是否确认删除" + (row?.name ?? "该条数据") + "？";
      this.$modal
        .confirm(text)
        .then(() => {
          that.$store.commit("app/updateApiCount", "add");
          delWhiteBlack({ id: row.id })
            .then((res) => {
              that.$store.commit("app/updateApiCount");
              if (res.code === 700) {
                that.$message.warning(res.msg);
                return false;
              }

              that.$message.success("已删除!");
              that.initData();
            })
            .catch((err) => {
              that.$store.commit("app/updateApiCount");
              // this.$message.error("系统错误,请联系管理员!301008");
            });
        })
        .catch(() => {});
    },
    async initData(currentPage, pageSize, data) {
      let that = this;
      let query = {
        ...data,
      };
      if (currentPage) {
        query.pageNum = currentPage;
      } else {
        query.pageNum = that.currentPage;
      }
      if (pageSize) {
        query.pageSize = pageSize;
      } else {
        query.pageSize = that.pageSize;
      }
      that.$store.commit("app/updateApiCount", "add");
      await whiteBlackList(query)
        .then((res) => {
          that.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            that.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res.rows;
          that.total = res.total;
        })
        .catch((err) => {
          that.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!301004");
        });
    },
    handlerSearchDetail(val) {
      //val===1 删除操作成功后分页边界值处理
      let currentPage = 1;
      if (val === 1) {
        if (
          this.total === (this.currentPage - 1) * this.pageSize + 1 &&
          this.total !== 0 &&
          this.currentPage > 1
        ) {
          currentPage = this.currentPage - 1;
          this.currentPage = currentPage;
        } else {
          currentPage = this.currentPage;
        }
      } else {
        currentPage = this.currentPage;
      }
      const data = {
        ...this.$refs?.form?.getFieldsValue(),
      };
      this.initData(currentPage, this.pageSize, data);
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    handleReset() {
      //重置
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
  },
};
</script>

<style scoped lang="scss">
@import "@/assets/styles/mixin.scss";
::v-deep .flex-row {
  display: flex;
  .left {
    width: 220px;
    max-height: 800px;
    margin-right: 20px;
    overflow: auto;
    @include scrollBar2;
    .el-tree {
      @include scrollBar2;
    }
  }
  .right {
    flex: 1;
    width: 100%;
    max-height: 800px;
    overflow: auto;
    @include scrollBar2;
  }
}
</style>
